﻿Public Class FrmMain
#Region "Attributes"
    Private mInventory As Inventory = New Inventory()
    Private Const mDEFAULT_FILE_NAME As String = "input.txt"
#End Region

#Region "Events"
    Private Sub _btnReadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFromFile.Click

        Try
            mInventory.readMultiLineRecordsFromFile(mDEFAULT_FILE_NAME)
            For i As Integer = 0 To mInventory.numInventoryItems - 1
                cboID.Items.Add(mInventory.ith(i).ID)
            Next
        Catch ex As Inventory_ErrorReadingFromFile_Exception
            MessageBox.Show("Could not open file: " & ex.ToString)
        Catch ex As Exception
            MessageBox.Show("Generic error" & ex.ToString)
        End Try
    End Sub

    Private Sub btnAddInventory_Click(sender As System.Object, e As System.EventArgs) Handles btnAddInventory.Click
        Dim id As String = mInventory.GenerateID()
        Dim description As String = txtDescription.Text
        Dim purchaseCost As Decimal = Decimal.Parse(txtPurchaseCost.Text)
        Dim salePrice As Decimal = Decimal.Parse(txtSalePrice.Text)
        Dim quantityOnHand As Integer = Integer.Parse(txtQtyOnHand.Text)
        Dim reorderPoint As Integer = Integer.Parse(txtReorderPoint.Text)
        Dim reorderQuantity As Integer = Integer.Parse(txtReorderQty.Text)

        Dim inventoryItem As New InventoryItem(id, description, purchaseCost, salePrice, quantityOnHand, reorderPoint, reorderQuantity)

        Try
            mInventory._addInventoryItem(inventoryItem)
        Catch ex As Inventory_InventoryItemAlreadyExists_Exception
            MessageBox.Show("ERROR: " & ex.ToString)
        End Try        
    End Sub

    Private Sub cboID_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboID.SelectedIndexChanged
        Dim id As String = cboID.SelectedItem.ToString
        Dim item As InventoryItem = mInventory.find(id)
        If IsNothing(item) = True Then
            MessageBox.Show("Not found")
        Else
            txtDescription.Text = item.Description
            txtPurchaseCost.Text = item.PurchaseCost
            txtQtyOnHand.Text = item.QuantityOnHand
            txtReorderPoint.Text = item.ReorderPoint
            txtReorderQty.Text = item.ReorderQuantity
            txtSalePrice.Text = item.SalePrice
        End If
    End Sub
#End Region
End Class
